package com.hupu.jrs.data

import android.text.TextUtils
import com.hupu.jrs.Constants
import com.hupu.jrs.api.forum.ForumApi
import com.hupu.jrs.bean.ThreadListData
import javax.inject.Inject
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Func0
import rx.schedulers.Schedulers
import rx.subjects.PublishSubject

/**
 * Created by sll on 2016/6/3.
 */
class ThreadRepository @Inject
constructor(private val mThreadDao: ThreadDao, private val mForumApi: ForumApi) {

    fun getThreadListObservable(type: Int,
                                mSubject: PublishSubject<List<Thread>>): Observable<List<Thread>> {
        val firstObservable = Observable.fromCallable(Func0<List<Thread>> { mThreadDao.queryBuilder().where(ThreadDao.Properties.Type.eq(type)).list() })
        return firstObservable.concatWith(mSubject)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
    }

    fun getRecommendThreadList(lastTid: String, lastTamp: String,
                               mSubject: PublishSubject<List<Thread>>?): Observable<ThreadListData> {
        return mForumApi.getRecommendThreadList(lastTid, lastTamp)
                .doOnNext { threadListData ->
                    if (threadListData != null && threadListData.result != null) {
                        cacheThreadList(0, TextUtils.isEmpty(lastTid), threadListData.result!!.data)
                    }
                    mSubject?.onNext(mThreadDao.queryBuilder()
                            .where(ThreadDao.Properties.Type.eq(Constants.TYPE_RECOMMEND))
                            .list())
                }
                .observeOn(AndroidSchedulers.mainThread())
    }

    fun getThreadsList(fid: String, lastTid: String,
                       lastTamp: String, type: String, mSubject: PublishSubject<List<Thread>>?): Observable<ThreadListData> {
        return mForumApi.getThreadsList(fid, lastTid, lastTamp, type)
                .doOnNext { threadListData ->
                    if (threadListData != null && threadListData.result != null) {
                        cacheThreadList(Integer.valueOf(fid)!!, TextUtils.isEmpty(lastTid),
                                threadListData.result!!.data)
                    }
                    mSubject?.onNext(mThreadDao.queryBuilder()
                            .where(ThreadDao.Properties.Type.eq(Integer.valueOf(fid)))
                            .list())
                }
                .observeOn(AndroidSchedulers.mainThread())
    }

    private fun cacheThreadList(type: Int, clear: Boolean, threads: List<Thread>?) {
        if (clear) {
            mThreadDao.queryBuilder()
                    .where(ThreadDao.Properties.Type.eq(type))
                    .buildDelete()
                    .executeDeleteWithoutDetachingEntities()
        }
        for (thread in threads!!) {
            if (mThreadDao.queryBuilder()
                    .where(ThreadDao.Properties.Tid.eq(thread.getTid()), ThreadDao.Properties.Type.eq(type))
                    .count() === 0) {
                thread.setType(type)
                mThreadDao.insert(thread)
            }
        }
    }
}
